package com.spring.dao.blog.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.spring.exception.DaoException;
import com.spring.bean.blog.Blog;
import com.spring.dao.blog.BlogDao;
import com.spring.bean.page.Page;
import com.spring.bean.page.PageView;
import com.spring.dao.DaoSupport;

@Repository
public class BlogDaoImpl extends DaoSupport implements BlogDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	private static final String TABLE_BLOG = "blog";

	/** insert */
	@Override
	public boolean insert(Blog blog) throws DaoException{
		try{
			return insert(jdbcTemplate, blog);
		} catch (Exception e) {
			throw new DaoException("Blog --- insert error : " + e.getMessage());
		}
	}

	/** udpate */
	@Override
	public boolean update(Blog blog) throws DaoException{
		try{
			return update(jdbcTemplate, blog);
		} catch (Exception e) {
			throw new DaoException("Blog --- update error : " + e.getMessage());
		}
	}

	/** delete */
	@Override
	public boolean delete(Integer id) throws DaoException{
		try{
			return deleteById(jdbcTemplate, TABLE_BLOG, id);
		} catch (Exception e) {
			throw new DaoException("Blog --- delete error : " + e.getMessage());
		}
	}

	/** getEntityById */
	@Override
	public Blog getById(Integer id) throws DaoException{
		try{
			return getById(jdbcTemplate, Blog.class , id);
		} catch (Exception e) {
			throw new DaoException("Blog --- query by id error : " + e.getMessage());
		}
	}

	/** queryForList */
	@Override
	public List<Blog> queryForList() throws DaoException{
		String sql = "select id,pb_id,sort_id,title,content,keyword,order_by,post_time,readed_num,commed_num,recommed_num,favorited_num,commed_able,is_able,close_reson from " + TABLE_BLOG;
		try{
			return queryForList(jdbcTemplate, Blog.class, sql);
		} catch (Exception e) {
			throw new DaoException("Blog --- query list error : " + e.getMessage());
		}
	}

	/** queryForPage */
	@Override
	public PageView<Blog> queryForPage(Page page) throws DaoException{
		String sql = "select SQL_CALC_FOUND_ROWS id,pb_id,sort_id,title,content,keyword,order_by,post_time,readed_num,commed_num,recommed_num,favorited_num,commed_able,is_able,close_reson from " + TABLE_BLOG;
		try{
			return queryForPage(jdbcTemplate, Blog.class, sql, page);
		} catch (Exception e) {
			throw new DaoException("Blog --- query page error : " + e.getMessage());
		}
	}
}